/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.domain;
/**
 *中药
 */
public class Herb {
 
	private int id = -1;
	 
	/**
	 *名称
	 */
	private String name;
	 
    /**
     * 原始剂量
     */
	private Unit dosage;
	
    /**
     *转换后剂量
     */
    private Unit unit;
    private boolean isTransformed;
    private String transformMsg;
    private static Herb h;
	 
	/**
	 *剂量折算
	 */
	public void transform() {
	}
    
    public Herb() {
        init();
    }
    
    public Herb(String str) throws FormatException {
        if ( str != null) {
            String[] names = str.split("\\d+\\.?\\d*");
            if (names.length == 2) {
                int begin =names[0].length();
                int end = str.lastIndexOf(names[1]);
                String value = "0";
                if (end > begin) {
                    value = str.substring(begin, end);
                }
                name = names[0].trim();
                dosage = new Unit(names[1].trim(), Float.parseFloat(value));
                unit = dosage;
            } else {
                throw new FormatException();
            }
            
            isTransformed = true;
        }
    }
    
    private void init() {
        this.isTransformed = false;
        this.transformMsg = "";
    }
    
    public Herb createEmptyNameHerb() {
        if (h == null) {
            h = new Herb();
        }
        h.setName("");
        h.setDosage(dosage);
        return h;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Unit getUnit() {
        return unit;
    }
    public void setUnit(Unit unit) {
        this.unit = unit;
    }
    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((name == null) ? 0 : name.hashCode());
        result = PRIME * result + ((unit == null) ? 0 : unit.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Herb other = (Herb) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (dosage == null) {
            if (other.dosage != null)
                return false;
        } else if (!dosage.equals(other.dosage))
            return false;
        return true;
    }

    public boolean isTransformed() {
        return isTransformed;
    }

    public void setTransformed(boolean isTransformed) {
        this.isTransformed = isTransformed;
    }
    
    public String getHtml(boolean original) {
        StringBuffer sb = new StringBuffer();
        sb.append("<td>");
        sb.append(this.name);
        sb.append("</td>");
        sb.append(this.unit.getHtml(original));
        if (!this.transformMsg.equals("")) {
            sb.append("<td>");
            sb.append(this.transformMsg);
            sb.append("</td>");
        }
        return sb.toString();
    }

    public String getTransformMsg() {
        return transformMsg;
    }

    public void setTransformMsg(String transformMsg) {
        this.transformMsg = transformMsg;
    }	 
    
    public String toString() {
        String s = "";
        if (unit != null) {
            s = name + unit.getQuantity() + unit.getUnit();
        }
        return s;
    }

    public Unit getDosage() {
        return dosage;
    }

    public void setDosage(Unit dosage) {
        this.dosage = dosage;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}